/*
 * Copyright (C) 2018 guomw.Inc. All rights reserved.
 * 项目名称： report-manage
 * 文件名称： ApiResult.java
 * Date：18-11-5 上午10:54
 * Author: guomw
 */

package com.guomw.report.common.utils;

import com.guomw.report.common.enums.ICommonEnum;
import lombok.Data;
import org.thymeleaf.util.StringUtils;

/**
 * @author guomw
 * @date 2018/9/6
 */
@Data
public class ApiResult<T>  {
    /**
     * 逻辑状态返回 ：1成功,0 失败.
     */
    private T resultCode;
    /**
     * 逻辑状态描述
     */
    private String resultMsg;

    public void setData(Object data) {
        this.data = data;
    }

    /**
     * 数据
     */
    private Object data;


    public static ApiResult resultWith(int code, String message) {
        ApiResult result =new ApiResult();
        result.setResultCode(code);
        result.setResultMsg(message);
        return result;
    }
    public static ApiResult resultWith(int code, String message, Object data) {
        ApiResult result =new ApiResult();
        result.setData(data);
        result.setResultCode(code);
        result.setResultMsg(message);
        return result;
    }


    public static <T extends ICommonEnum> ApiResult resultWith(T code) {
        return resultWith(code, "",null);
    }

    public static <T extends ICommonEnum> ApiResult resultWith(T code, String description,Object objData)  {
        ApiResult result = new ApiResult();
        result.setResultCode(code.getValue());
        if(StringUtils.isEmpty(description)) {
            result.setResultMsg(code.getName());
        } else {
            result.setResultMsg(description);
        }
        result.setData(objData);
        return result;
    }

    public static <T extends ICommonEnum> ApiResult resultWith(T code, String description) {

        return resultWith(code, description,null);
    }
    public static <T extends ICommonEnum> ApiResult resultWith(T code,Object objData) {

        return resultWith(code, null,objData);
    }



}
